<?php


namespace app\admin\controller\goods;


use app\common\controller\AdminController;
use app\common\model\Goods;
use app\common\model\GoodsLibraryGoodsRelation;
use app\common\model\GoodsSku;
use think\facade\Db;

class Goods2 extends AdminController
{

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            try {
                if (input('selectFields')) {
                    return $this->selectList();
                }
                $this->model = new Goods();
                list($page, $limit, $where) = $this->buildTableParames(['shop_name', 'price_low', 'price_high']);
                $filter = json_decode(input('filter'), true);
                if (isset($filter['shop_name'])) {
                    if ($filter['shop_name'] == "平台") {
                        $where[] = ['g.shop_id', '=', 0];
                    } else {
                        $where[] = ['s.name', 'like', '%' . $filter['shop_name'] . '%'];
                    }
                }

                $price_low = 0;
                if (isset($filter['price_low'])) {
                    $price_low = $filter['price_low'];
                    $where[]   = ['g.price_high', '>=', $price_low];
                }
                if (isset($filter['price_high'])) {
                    $price_high = $filter['price_high'];
                    if ($price_high < $price_low) {
                        return json([
                            'code' => 1,
                            'msg'  => '价格最大值不能小于价格最小值',
                        ]);
                    }
                    $where[] = ['g.price_low', '<=', $price_high];
                }


                $count = $this->model->alias('g')
                    ->leftJoin('shop s', 's.id=g.shop_id')
                    ->leftJoin('pm_goods_category gc', 'g.category_id = gc.id')
                    ->where($where)
                    ->count();
                $list  = $this->model->alias('g')
                    ->leftJoin('shop s', 's.id=g.shop_id')
                    ->leftJoin('pm_goods_category gc', 'g.category_id = gc.id')
                    ->field('g.*,s.name as shop_name,gc.name as category_name')
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();


                foreach ($list as $k => $l) {
                    if ($l['shop_id'] == 0) {
                        $list[$k]['shop_name'] = '平台';
                    }
                }
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => $count,
                    'data'  => $list,
                ];
            } catch (\Exception $e) {
                dd($e->getMessage());
            }
            return json($data);
        }
        $this->assign('library_id', input('library_id'));
        return $this->fetch();
    }

    /**
     * 获取商品多规格下面的详细库存
     */
    public function stockList()
    {
        $id   = input('id');
        $list = GoodsSku::where('goods_id', $id)->field('')->select();
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
    }

    public function addToLibrary()
    {
        $id         = input('id');
        $library_id = input('library_id', 0);
        if ($library_id == 0) {
            return json(['code' => 1, 'msg' => '请重新选择商品库']);
        }
        if (count($id) < 1) {
            return json(['code' => 1, 'msg' => '请选择商品']);
        }
        try{
            $goods_ids = GoodsLibraryGoodsRelation::where('goods_library_id',$library_id)->column('goods_id');
            Db::startTrans();
            foreach($id as $v){
                if(!in_array($v,$goods_ids)){
                    GoodsLibraryGoodsRelation::create([
                        'goods_library_id' => $library_id,
                        'goods_id' => $v,
                    ]);
                }
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            dd($e->getMessage());
        }
        return json(['code' => 1, 'msg' => '添加成功']);
    }
}